﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;
using DSPL.Contracts;
using DSPL.Contracts.Common;
using DSPL.Contracts.Interfaces;
using System.Diagnostics;
using DSPL.DataAccess;
using DSPL.Services.Services;
using DSPL.DataAccess.MasterData;

namespace DSPL.Services.Rules
{
    public class CommonRuleService : ICommonRules
    {
        private CommonRules _commonRules;
        public CommonRuleService()
        {
            _commonRules = new CommonRules();
        }

        #region Mline_Tax_Cal

        /// <summary>
        ///  Multiple Line Tax Calculation
        /// </summary>
        /// <param name="dtTranDet">Transaction Detail table (Temporary)</param>
        /// <param name="dtTaxDetailTable">Tax transaction table</param>
        /// <param name="curRate"> Currency exchange rate</param>
        /// <param name="taxDetailTranTable"> Link Transaction Tax Detail Table (Leave Blank to pick tax from Tax detail table) </param>
        /// <param name="taxRegState"></param>
        /// <param name="totalTax">out parameter to get the Total Tax Amount</param>
        /// <param name="totAmount">out parameter to get the Total Amount</param>
        /// <param name="referenceNo">Link Transaction Reference No (Leave Blank if multiple references (Grid Fields “RefNo1”) OR Reference No to pick Tax details)</param>
        /// <param name="whereCondition">Record Validation (To check valid line records)</param>
        /// <param name="keyFields">Tax transaction table key fields (Support multiple character type fields OR Leave blank for if key field is not required)</param>
        /// <param name="isTaxReverse"> Reverse Tax Flag (true – Reverse, false – Forward Calculation)   </param>
        /// <returns>Update some out parameters and return true or false</returns>
        public bool MultipleLineTaxCalculation(ref DataTable dtTranDet, ref DataTable dtTaxDetailTable, double curRate,
            out double totalTax, out double totAmount, int taxRegState = 0, string referenceNo = "", string taxDetailTranTable = "", string whereCondition = "", string keyFields = "", bool isTaxReverse = false)
        {

            return _commonRules.MultipleLineTaxCalculation(ref dtTranDet, ref  dtTaxDetailTable, curRate,
             out  totalTax, out  totAmount, taxRegState, referenceNo, taxDetailTranTable, whereCondition, keyFields, isTaxReverse);


        }

        #endregion
       
        #region sLine_Tax_cal

        /// <summary>
        /// Single Line Tax Calculation
        /// </summary>
        /// <param name="dtDetailTable">Temporary -Tax Detail Table</param>
        /// <param name="taxCombineCode">Tax Combination Code</param>
        /// <param name="amount">Tax Amount</param>
        /// <param name="curRate">Currency Rate</param>
        /// <param name="keyCodeValues">List of key fields with Values</param>
        /// <param name="linkReferenceNo">Link Reference No</param>
        /// <param name="taxTranDetTable">Tax Transaction Table Temporary Table</param>
        /// <param name="isReverse">Whether to reverse the transaction or not - true or false</param>
        /// <param name="referenceNo">Reference No</param>
        /// <returns>returns the tax value</returns>
        public double SingleLineTaxCalculation(ref DataTable dtDetailTable, string taxCombineCode, double amount,
            double curRate, List<KeyValuePair<string, object>> keyCodeValues = null, string linkReferenceNo = "", string taxTranDetTable = "", bool isReverse = false, string referenceNo = "")
        {

            return _commonRules.SingleLineTaxCalculation(ref  dtDetailTable, taxCombineCode, amount,
            curRate, keyCodeValues, linkReferenceNo, taxTranDetTable, isReverse, referenceNo);
        }
        #endregion


        #region Get_Latest_Rate

        public double GetLatestCurrencyRate(string currencyCode, string baseCurrency, DateTime transactionDate)
        {
            return _commonRules.GetLatestCurrencyRate(currencyCode, baseCurrency, transactionDate);

        }
        #endregion


        #region Auto_Gen_No
        /// <summary>
        /// Auto Generate the refernce number accrding to the specified type in the 
        /// 'fCompanySetting' table
        /// </summary>
        /// <param name="moduleCode"> Unique Code specifed in the fCompanySetting table</param>
        /// <param name="transactionDate">transaction date</param>
        /// <param name="branchCode">Branch Code</param>
        /// <param name="isViewOnly"></param>
        /// <returns>newly generated reference number</returns>
        public string AutoGenerateNo(string moduleCode, DateTime transactionDate, string branchCode = "", bool isViewOnly = false)
        {
            return _commonRules.AutoGenerateNo(moduleCode, transactionDate, branchCode, isViewOnly);
        }

        #endregion


        #region Validate_Duplicates

        public bool ValidateDuplicates(string caption, string tableName, string dupFieldName, string dupFieldValue, string keyFieldName, string keyFieldValue)
        {
            return _commonRules.ValidateDuplicates(caption, tableName, dupFieldName, dupFieldValue, keyFieldName, keyFieldValue);
        }
        #endregion


        #region Validate Currency Rate

        public double ValidateCurrencyRate(string currencyName, double curerencyRate)
        {
            return _commonRules.ValidateCurrencyRate(currencyName, curerencyRate);
        }
        #endregion


        #region Val_Bank_Prefix

        public string ValidateBankPrefix(string accountCode, string extendedPrefix = "")
        {
            return _commonRules.ValidateBankPrefix(accountCode, extendedPrefix);
        }

        public bool WriteLogManual(string headerTable, string detailTable, string logTableName, string keyFieldName, string keyFieldValue, SqlTransaction sqlTransaction, string reason = "", string logType = "D")
        {
            return _commonRules.WriteLogManual(headerTable, detailTable, logTableName, keyFieldName, keyFieldValue, sqlTransaction, reason, logType);

        }
         
        #endregion
    }
}
